var alerthtml = `<div class="dialog" id="dialog-alert">
                    <div class="frame">
                        <div class="title">提示</div>
                        <div class="content">确认型弹窗</div>
                        <div class="btn">
                            <button class="ok">确定</button>
                        </div>
                    </div>
                    </div>`
var confirmhtml = `<div class="dialog" id="dialog-confirm">
                    <div class="frame">
                        <div class="title">温馨提示</div>
                        <div class="content">询问弹窗</div>
                        <div class="btn" style="display: flex;">
                            <button class="no">取消</button>
                            <button class="ok">确定</button>
                        </div>
                    </div>
                    </div>`
var tipshtml = `<div class="dialog" id="dialog-tips" style="background: none;">
                    <div class="content tips">提示型弹窗</div>
                </div>`
var successhtml = `<div class="dialog" id="dialog-success">
                    <div class="frame">
                        <div class="image" style="width: 100px;height: 100px;margin: 10px auto;">
                            <img src="http://ku.90sjimg.com/element_origin_min_pic/01/32/21/68573b6aad1d463.jpg" alt="" style="width: 100%;">
                        </div>
                        <div class="btn">
                            <div class="content">操作成功</div>
                        </div>
                    </div>
                    </div>`
var failhtml = `<div class="dialog" id="dialog-fail">
                <div class="frame">
                    <div class="image" style="width: 100px;height: 100px;margin: 10px auto;">
                        <img src="https://i02piccdn.sogoucdn.com/8d272998af9d0d5b" alt="" style="width: 100%;">
                    </div>
                    <div class="btn">
                        <div  class="content">操作失败</div>
                    </div>
                </div>
                </div>`

let dialoghtml = {
    'alert': alerthtml,
    'confirm': confirmhtml,
    'tips': tipshtml,
    'success': successhtml,
    'fail': failhtml
}
var dialog = function () {
    return {
        // 打开
        open: function (id, txt = '') {
            $('body').append(dialoghtml[id])
            let obj = $("#dialog-" + id)
            $("#dialog-" + id).fadeIn(500)
            $(obj).find('.frame').fadeIn(600)
            if (txt != '') {
                $(obj).find('.content').text(txt)
            }
        },

        // 普通弹窗
        alert: function (txt = '', callback) {
            this.open('alert', txt);
            let that = this;
            $('body').off('click').on('click', '.btn', function () {
                that.close('alert');
                if (typeof (callback) != 'undefined') {
                    callback();
                }
            })
        },

        // 询问弹窗
        confirm: function (txt = '', confirm, concel) {
            this.open('confirm', txt)
            let that = this;
            $('body').off('click').on('click', '.btn button', function () {
                that.close('confirm');
                if ($(this).attr("class") == 'ok') {
                    confirm();
                } else {
                    concel();
                }
            })
        },

        // 提示型弹窗
        tips: function (txt) {
            this.open('tips', txt)
            let that = this;
            setTimeout(function () {
                that.close('tips');
            }, 1500)
        },


        // 显示
        loading: function (txt) {
            this.open('alert', txt);
        },

        // 隐藏
        hideLoading() { },

        // 操作成功提示
        success: function (txt = '', callback, timer = 1500) {
            this.open('success', txt)
            let that = this;
            if (typeof (callback) != 'undefined') {
                callback();
            }
            setTimeout(function () {
                that.close('success')
            }, timer)
        },

        // 操作失败提示
        fail: function (txt = '', callback, timer = 1500) {
            this.open('fail', txt)
            let that = this;
            if (typeof (callback) != 'undefined') {
                callback();
            }
            setTimeout(function () {
                that.close('fail');
            }, timer)
        },

        // 关闭
        close: function (id) {
            let obj = $("#dialog-" + id)
            obj.remove();
        },

        // 关闭所有
        closeAll() { }
    }
}();


class Notify{
    // 构造器
    constructor(){
        this.stop = true;
        $('body').remove(".notify").append(`<div class="notify">Notify 消息提示</div>`)
    }
    // 主要通知
    primary(txt, timer = 1500){
        if (this.stop) {
            this.stop = false
            $(".notify").css({ "background": "#1989FA", "color": "#fff" }).text(txt).slideDown("slow")
            this.delay(timer);
        }
    }
    // 成功通知
    success(txt, timer = 1500) {
        if (this.stop) {
            this.stop = false
            $(".notify").css({ "background": "#07C160", "color": "#fff" }).text(txt).slideDown("slow")
            this.delay(timer);
        }
    }
    // 危险通知
    danger(txt, timer = 1500) {
        if (this.stop) {
            this.stop = false
            $(".notify").css({ "background": "#FF976A", "color": "#fff" }).text(txt).slideDown("slow")
            this.delay(timer);
        }
    }
    // 警告通知
    warning(txt, timer = 1500) {
        if (this.stop) {
            this.stop = false
            $(".notify").css({ "background": "#EE0A24", "color": "#fff" }).text(txt).slideDown("slow")
            this.delay(timer);
        }
    }
    // 自定义
    custom(txt, option = { background: "#FFE1E1", color: "#B40000" }, timer = 1500) {
        if (this.stop) {
            this.stop = false
            $(".notify").css(option).text(txt).slideDown("slow")
            this.delay(timer);
        }
    }
    // 关闭
    delay(timer){
        let that = this;
        setTimeout(function () {
            $(".notify").slideUp("slow");
            $('body').remove(".notify")
            that.stop = true;
        }, timer)
    }
}

var notify = new Notify()


